/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.internal;

import org.openeccos.util.PDUtil;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.ApplicationInstance;
import nextapp.echo2.app.Border;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.FillImage;
import nextapp.echo2.app.Grid;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.layout.RowLayoutData;
import nextapp.echo2.contrib.app.effect.ScaleEffectCommand;
import echopointng.ButtonEx;
import echopointng.ContainerEx;
import echopointng.LabelEx;

/**
 *
 *
 * @author cgspinner@web.de
 */
public class PDExpandableFilter extends ContainerEx {

	private Grid grid;
	private ButtonEx btnExpand;
	private boolean expanded = false;
	private ButtonEx btnSearch;
	private ButtonEx btnReset;
	private ActionListener okListener;
	private ActionListener resetListener;

	public PDExpandableFilter(ActionListener okListener, ActionListener resetListener) {
		this.okListener = okListener;
		this.resetListener = resetListener;
		initGUI();
	}

	private void initGUI() {
		setInsets(new Insets(0, 21, 0, 0));
		setLeft(new Extent(-401)); // most part is invisible when not expanded!!!
		setWidth(new Extent(20));
		setBorder(new Border(new Extent(0), null, Border.STYLE_NONE));

		// this row contains in this order: the pusher, the filter grid and the
		// show/hide button
		// the pusher (100px) and the grid (300px) are initially invisible,
		// as the x-coordinates are moved 400px to the left (see above)
		// the pusher expands from 100px to 400px (=400%), and pushes the grid
		// into the frame
		final Row row = new Row();
		add(row);

		// this component is never visible
		// it is just there to use more or less space
		final ContainerEx pusher = new ContainerEx();
		pusher.setWidth(new Extent(100));
		row.add(pusher);

		grid = new Grid(2);
		grid.setInsets(new Insets(3, 3, 2, 3));
		grid.setWidth(new Extent(290));
		RowLayoutData rld = new RowLayoutData();
		rld.setBackgroundImage(new FillImage(PDUtil.getImage("img/semitrans4.png")));
		rld.setAlignment(new Alignment(Alignment.TOP, Alignment.TOP));
		rld.setInsets(new Insets(5));
		grid.setLayoutData(rld);
		row.add(grid);

		Column colCommands = new Column();
		colCommands.setBackground(Color.ORANGE);
		rld = new RowLayoutData();
		rld.setAlignment(new Alignment(Alignment.TOP, Alignment.TOP));
		colCommands.setLayoutData(rld);
		row.add(colCommands);

		btnExpand = new ButtonEx("F\ni\nl\nt\ne\nr");
		btnExpand.setInterpretNewlines(true);
		btnExpand.setBackground(Color.ORANGE);
		btnExpand.setForeground(Color.DARKGRAY);
		btnExpand.setInsets(new Insets(6, 12, 0, 0));
		btnExpand.setBorder(new Border(new Extent(1), Color.DARKGRAY, Border.STYLE_DOTTED));
		btnExpand.setHeight(new Extent(110));
		btnExpand.setWidth(new Extent(14));
		btnExpand.setRolloverEnabled(false);
		btnExpand.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ScaleEffectCommand cmd = new ScaleEffectCommand();
				expanded = !expanded;
				if (expanded) {
					cmd.setStartScalePercentage(100f);
					cmd.setEndScalePercentage(400);
					btnExpand.setText("H\ni\nd\ne");
				} else {
					cmd.setStartScalePercentage(100f);
					cmd.setEndScalePercentage(25);
					btnExpand.setText("F\ni\nl\nt\ne\nr");
				}
				btnReset.setVisible(expanded);
				btnSearch.setVisible(expanded);
				cmd.setComponent(pusher);
				ApplicationInstance.getActive().enqueueCommand(cmd);
			}
		});
		colCommands.add(btnExpand);

		btnReset = new ButtonEx(PDUtil.getImage("img/undo.gif"));
		btnReset.setBorder(new Border(0, null, Border.STYLE_NONE));
		btnReset.setBackground(Color.ORANGE);
		btnReset.setRolloverEnabled(false);
		btnReset.setVisible(false);
		btnReset.addActionListener(resetListener);
		colCommands.add(btnReset);

		btnSearch = new ButtonEx(PDUtil.getImage("img/generado.gif"));
		btnSearch.setBorder(new Border(0, null, Border.STYLE_NONE));
		btnSearch.setBackground(Color.ORANGE);
		btnSearch.setRolloverEnabled(false);
		btnSearch.setInsets(new Insets(0, 0, 0, 5));
		btnSearch.addActionListener(okListener);
		btnSearch.setVisible(false);
		colCommands.add(btnSearch);

	}

	public void addFilter(String caption, Component filterComponent) {
		grid.add(new LabelEx(caption));
		grid.add(filterComponent);
	}
}
